<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-lx-cascades"></i> 点单订单</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                    <!-- <el-button type="primary" icon="delete" class="handle-del mr10" @click="delAll">批量删除</el-button> -->
                    <el-select v-model="select_cate" clearable placeholder="筛选条件" class="handle-select mr10" @change="changeIn()">
                        <el-option key="1" label="日期时间" value="date"></el-option>
                        <!-- <el-option key="2" label="订单编号" value="order"></el-option> -->
                        <el-option key="2" label="手机号" value="phone"></el-option>
                        <el-option key="3" label="价格" value="price"></el-option>
                    </el-select>
                    <el-input v-model="select_word" placeholder="筛选关键词" class="handle-input mr10" v-if="select_cate != 'date'"></el-input>
                    <!-- <el-input v-model="select_word" placeholder="筛选关键词" class="handle-input mr10" ></el-input> -->
                    <el-date-picker v-model="select_word" type="date" placeholder="选择日期" v-if="select_cate == 'date'"> </el-date-picker>
                    <el-button type="primary" icon="search" @click="search">搜索</el-button>
                </div>                    
                     <el-table :data="tableData" border class="table" ref="multipleTable" @selection-change="handleSelectionChange">
                        <!-- <el-table-column type="selection" width="55" align="center"></el-table-column> -->
                       <!--  <el-table-column prop="transaction_id" label="支付单号" align="center" width="250">
                        </el-table-column> -->
                        <el-table-column prop="pay_time" label="支付时间" align="center" width="155">
                        </el-table-column>
                        <el-table-column prop="list_num" label="领取编号" align="center" show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column prop="order_spec" label="订单详情" align="center" min-width="360">
                             <template slot-scope="scope" >
                                <div v-for="(item,index) in scope.row.order_spec" style="text-align: left;">
                                    {{index+1}}.
                                    名称：<span><strong>{{item.good_name}}</strong></span>；
                                    <span v-if="item.spec.length > 0">
                                        <span v-for="(i,v) in item.spec">
                                            <span>{{i.spec}}，</span>
                                        </span>
                                    </span>
                                    价格：￥<span>{{item.price}}</span>元；
                                    数量：<span><strong>{{item.num}}</strong></span>个
                                </div>
                            </template>
                        </el-table-column>
                         <el-table-column prop="price" label="支付金额" align="center" show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column prop="cate" label="下单类型" align="center" width="120">
                        </el-table-column>
                        <el-table-column prop="contact" label="下单用户电话" align="center" width="120">
                             <template slot-scope="scope" >
                                <div v-if="scope.row.cate !='外卖订单'">
                                    {{scope.row.contact}}
                                </div>
                                <!-- <div v-if="scope.row.cate =='外卖订单'">
                                    {{scope.row.contact}}
                                </div> -->
                            </template>
                        </el-table-column>
                       <!--  <el-table-column prop="address" label="下单用户地址信息" align="center" width="480">
                        </el-table-column> -->

                        <el-table-column prop="pay_msg" label="是否支付" align="center">
                          <!--   <template slot-scope="scope" >
                                 <el-radio-group v-model="scope.row.is_pay">
                                      <el-radio :label="0" disabled>待支付</el-radio>
                                      <el-radio :label="1" disabled>已支付</el-radio>
                                      <el-radio :label="2" disabled>已取消</el-radio>
                                    </el-radio-group>
                            </template> -->
                        </el-table-column>

                         <el-table-column prop="is_seek" label="是否领取" align="center">
                            <template slot-scope="scope" >
                                <!--  <el-radio-group v-model="scope.row.is_pay">
                                      <el-radio :label="0" disabled>待支付</el-radio>
                                      <el-radio :label="1" disabled>已支付</el-radio>
                                      <el-radio :label="2" disabled>已取消</el-radio>
                                    </el-radio-group> -->
                                    <el-switch v-model="scope.row.is_seek" active-color="#13ce66" inactive-color="#ff4949" :disabled="scope.row.is_pay != 1|| scope.row.is_seek == true ? true:false" @change="rack(scope.$index, scope.row)">
                                    </el-switch>
                            </template>
                        </el-table-column>
                        <el-table-column fixed='right' label="操作" align="center" width="80">
                            <template slot-scope="scope">
                                <!-- <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button> -->
                                <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
            <div class="pagination">
                <el-pagination background @current-change="handleCurrentChange" layout="prev, pager, next" :total="total">
                </el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="100px">
                <el-form-item label="日期">
                    <el-date-picker type="date" placeholder="选择日期" v-model="form.date" value-format="yyyy-MM-dd" style="width: 100%;"></el-date-picker>
                </el-form-item>
                <el-form-item label="姓名">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="地址">
                    <el-input v-model="form.address"></el-input>
                </el-form-item>

            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 删除提示框 -->
        <el-dialog title="提示" :visible.sync="delVisible" width="300px" center>
            <div class="del-dialog-cnt">删除不可恢复，是否确定删除？</div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="delVisible = false">取 消</el-button>
                <el-button type="primary" @click="deleteRow">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        inject : ['reload'],
        name: 'basetable',
        data() {
            return {
                tableData: [],
                cur_page: 1,
                multipleSelection: [],
                select_cate: '',
                select_word: '',
                del_list: [],
                is_search: false,
                editVisible: false,
                delVisible: false,
                form: {

                },
                idx: -1,
                admininfo:JSON.parse(localStorage.getItem('ms_username')),
                total:1,
            }
        },
        created() {
            this.getData();
        },
        computed: {
            data() {
                return this.tableData.filter((d) => {
                    let is_del = false;
                    for (let i = 0; i < this.del_list.length; i++) {
                        if (d.o_id === this.del_list[i].o_id) {
                            is_del = true;
                            break;
                        }
                    }
                    if (!is_del) {
                        if (d.o_id.indexOf(this.select_word) > -1 ) {
                            return d;
                        }
                    }
                })
            }
        },
        methods: {
            // 分页导航
            handleCurrentChange(val) {
                this.cur_page = val;
                this.getData();
            },
            // formatInfo(row,column){
            //     var str = '';
            //     for(var i=0 ; i < row.order_spec.length ; i++){
            //         // for(var j = 0; j <= row.order_spec[i].o_spec; j++){

            //             str +=  i+1+'商品:'+row.order_spec[i].good_name.good_name+',规格【加料：'+
            //             row.order_spec[i].o_spec[0].feed+'】,数量：'+row.order_spec[i].num+';'
            //         // }
            //     }
            //     // for(var i=0 ; i<= row.order_spec[0].o_spec[0].length ; i++){

            //     // }
            //     // return row.order_spec[0].o_spec[0].feed;
            //     return str;

            // },
            // 获取 easy-mock 的模拟数据
            getData() {
                // 开发环境使用 easy-mock 数据，正式环境使用 json 文件
                // if (process.env.NODE_ENV === 'development') {
                //     this.url = '/ms/table/list';
                // };
                 this.$axios.get("admin/order/lie", {params:{
                        aid: this.admininfo['admin_id'],
                        token: this.admininfo['token'],
                        page: this.cur_page
                    }}).then((res) => {
                        console.log(res.data.data)

                        if(res.data.status == 0){
                            this.tableData = res.data.data.data;
                            this.total = res.data.data.total 
                            this.current_page = res.data.data.current_page 
                        }else if(res.data.status == 3){
                           this.$router.push('login') 
                        }
                        
                    })
            },
            search() {
                if(this.select_cate == 'date' && this.select_word != ''){

                    var d = new Date(this.select_word);
                    this.select_word = d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate()
                }else if(this.select_cate == 'date' && this.select_word == ''){
                   this.select_word = ''; 
                }
                this.is_search = true;
                    this.$axios.get("admin/order/search", {params:{
                        aid: this.admininfo['admin_id'],
                        token: this.admininfo['token'],
                        page: this.cur_page,
                        type: this.select_cate,
                        keyWord: this.select_word,
                        head:'1'
                    }}).then((res) => {

                        if(res.data.status == 0){
                            this.tableData = res.data.data.data;
                            // console.log(this.tableData);
                            this.$message.success(`有${res.data.data.total}条`);
                            // this.$refs.multipleTable.dolayout();
                        }else if(res.data.status == 5){
                            this.$message.success(`没有数据`);
                        }else{
                            this.$message.error(res.data.msg)
                        }
                        
                    })
            },
            formatter(row, column) {
                return row.address;
            },
            filterTag(value, row) {
                return row.tag === value;
            },
            handleEdit(index, row) {
                this.idx = index;
                const item = this.tableData[index];
                this.form = {
                    name: item.name,
                    date: item.date,
                    address: item.address
                }
                this.editVisible = true;
            },
            handleDelete(index, row) {
                this.idx = index;
                this.delVisible = true;
            },
            delAll() {
                const length = this.multipleSelection.length;
                let str = '';
                this.del_list = this.del_list.concat(this.multipleSelection);
                for (let i = 0; i < length; i++) {
                    str += this.multipleSelection[i].name + ' ';
                }
                this.$message.error('删除了' + str);
                this.multipleSelection = [];
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            // 保存编辑
            saveEdit() {
                this.$set(this.tableData, this.idx, this.form);
                this.editVisible = false;
                this.$message.success(`修改第 ${this.idx+1} 行成功`);
            },
            // 确定删除
            deleteRow(){
                this.tableData.splice(this.idx, 1);
                this.$message.success('删除成功');
                this.delVisible = false;
            }
        }
    }

</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .del-dialog-cnt{
        font-size: 16px;
        text-align: center
    }
    .table{
        width: 100%;
        font-size: 14px;
    }
    .red{
        color: #ff0000;
    }
</style>
